﻿#include "DAGraphicsInfo.h"

#include <QDebug>
#include <QHBoxLayout>
#include <QPainter>
#include <QRect>

DAGraphicsInfo::DAGraphicsInfo(QWidget *parent)
    : QWidget{parent}
{
    timer = new QTimer(this);
    LabelGPU.setParent(this);
    LabelGPU.setObjectName("LabelGPU");
    LabelGPU.setText("GPU：");
    LabelGPU.setFixedWidth(50);

    LabelGMEM.setParent(this);
    LabelGMEM.setObjectName("LabelGMEM");
    LabelGMEM.setText("GME：");
    LabelGMEM.setFixedWidth(50);

    GPURate.setParent(this);
    GPURate.setObjectName("GPURete");
    GPURate.setText("0%");
    GPURate.setFixedWidth(80);
    GMEMRate.setParent(this);
    GMEMRate.setObjectName("GMEMRete");
    GMEMRate.setText("0%");
    GMEMRate.setFixedWidth(80);

    QHBoxLayout *hbl = new QHBoxLayout();
    hbl->addWidget(&LabelGPU);
    hbl->addWidget(&GPURate);
    hbl->addWidget(&LabelGMEM);
    hbl->addWidget(&GMEMRate);
    setLayout(hbl);
#if HAVE_NVIDIA_ML
    // init nvidia library
    result = nvmlInit();
    result = nvmlDeviceGetCount(&device_count);
    connect(timer,&QTimer::timeout,[&](){
        Refresh();
    });
    timer->start(2000);
#endif

}

void DAGraphicsInfo::setGPUUsed(unsigned int rate)
{
    if ( rate < 50.0)
    {
        GPURate.setStyleSheet("QLabel{ color:rgb(255,255,255)}");
    }
    if (rate > 50.0)
    {
        GPURate.setStyleSheet("QLabel{ color:rgb(255,215,0)}");
    }
    if (rate > 80.0)
    {
        GPURate.setStyleSheet("QLabel{ color:rgb(255,0,0)}");
    }
    GPURate.setText(QString("%1%").arg(rate));
}

void DAGraphicsInfo::setGMEMUsed(unsigned int rate)
{
    if (rate < 50)
    {
        GMEMRate.setStyleSheet("QLabel{ color:rgb(255,255,255)}");
    }
    if (rate > 50)
    {
        GMEMRate.setStyleSheet("QLabel{ color:rgb(255,215,0)}");
    }
    if (rate > 80)
    {
        GMEMRate.setStyleSheet("QLabel{ color:rgb(255,0,0)}");
    }
    GMEMRate.setText(QString("%1%").arg(rate));

}

void DAGraphicsInfo::paintEvent(QPaintEvent *e)
{
    //创建绘画对象
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing); // 反锯齿;
    painter.setBrush(QBrush(QColor(0, 0, 0, 50)));
    painter.setPen(Qt::transparent);

    QRect rect = this->rect();
    rect.setWidth(rect.width()-1);
    rect.setHeight(rect.height()-1);
    painter.drawRoundedRect(rect,5,5);

    //    QPen pen;
    //    pen.setColor(Qt::red);
    //    pen.setWidth(0);
    //    painter.setPen(pen);
    //    painter.drawRect(this->rect());
    //    painter.end();
    QWidget::paintEvent(e);
}


void DAGraphicsInfo::Refresh()
{
#if HAVE_NVIDIA_ML
    if (NVML_SUCCESS != result)
    {
        qout << "Failed to query device count: " << nvmlErrorString(result);
        return ;
    }
    if(device_count == 0)
    {
        qout << "Found " << device_count <<" device";
        return;
    }

    for (i = 0; i < device_count; i++)
    {
        nvmlDevice_t device;
        char name[NVML_DEVICE_NAME_BUFFER_SIZE];
        result = nvmlDeviceGetHandleByIndex(i, &device);
        if (NVML_SUCCESS != result) {
            qout << "get device failed : " << nvmlErrorString(result);
            return;
        }
        result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
        if (NVML_SUCCESS != result) {
            qout << "get name failed ： " << nvmlErrorString(result) ;
                return;
        }
        //使用率
        nvmlUtilization_t utilization;
        result = nvmlDeviceGetUtilizationRates(device, &utilization);
        if (NVML_SUCCESS == result)
        {
            //            qout << "卡"<< i <<" [ " << name  << "]使用率 ";
            setGPUUsed(utilization.gpu);
            setGMEMUsed(utilization.memory);
            //            qout << "GPU 使用率： " << utilization.gpu << "  显存使用率 " << utilization.memory;
        }
    }
#endif
//    qout << "=============================";
}
